<template>
  <el-form :model="state.form" :inline="true" class="my-form" label-position="right" label-width="90px">
    <el-form-item label="活动编号">
      <el-input clearable v-model="state.form.activity_no" style="width: 145px;" placeholder="请输入活动编号" />
    </el-form-item>
    <el-form-item label="REC编号">
      <el-input clearable v-model="state.form.rec_activity_no" style="width: 145px;" placeholder="请输入REC编号" />
    </el-form-item>
    <el-form-item label="订单编号">
      <el-input clearable v-model="state.form.order_no" style="width: 145px;" placeholder="请输入订单编号" />
    </el-form-item>
    <el-form-item label="商家">
      <el-input clearable v-model="state.form.client_search" style="width: 145px;" placeholder="请输入商家名称" />
    </el-form-item>
    <el-form-item label="设备">
      <el-input clearable v-model="state.form.device_search" style="width: 145px;" placeholder="输入设备编号,名称" />
    </el-form-item>
    <el-form-item label="用户">
      <el-input clearable v-model="state.form.user_search" style="width: 145px;" placeholder="输入用户ID,邮箱" />
    </el-form-item>
    <el-form-item label="活动类型">
      <el-select clearable v-model="state.form.type" placeholder="请选择活动类型" style="width: 145px;">
        <el-option :label="item.val" :value="item.key" v-for="item in state.type" :key="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item label="活动状态">
      <el-select clearable v-model="state.form.status" placeholder="请选择活动状态" style="width: 145px;">
        <el-option :label="item.val" :value="item.key" v-for="item in state.status" :key="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item label="创建时间">
      <el-date-picker style="width: 385px;" v-model="state.selectData" :editable="false" start-placeholder="结束时间"
        end-placeholder="结束时间" type="daterange" range-separator="至" value-format="YYYY-MM-DD" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit" :loading="isLoading" icon="Search">搜索</el-button>
      <el-button type="primary" @click="onExport" :loading="isExporting" icon="Download">导出</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive, onMounted, watchEffect } from 'vue'
import { getActivityEnum } from '@/api/activityManage/activityManage';
import { useCommonStore } from '@/stores';
import { isEmpty } from 'lodash-es';

const props = defineProps({
  isLoading: {
    type: Boolean,
    default: false
  },
  isExporting: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['search', 'export'])

const commonStore = useCommonStore();

const state = reactive({
  form: {
    activity_no: '',
    rec_activity_no: '',
    order_no: '',
    client_search: '',
    user_search: '',
    device_search: '',
    start_time: '',
    end_time: '',
    type: '',
    status: ''
  },
  status: [],
  type: [],
  selectData: ''
})


watchEffect(() => {
  if (state.selectData) {
    state.form.start_time = state.selectData[0]
    state.form.end_time = state.selectData[1]
  } else {
    state.form.start_time = ''
    state.form.end_time = ''
  }
})

// methods
const onSubmit = () => {
  emit('search', state.form)
}

const onExport = () => {
  emit('export', state.form)
}


onMounted(async () => {
  if(!isEmpty(commonStore.activityEnum)){
    state.status = commonStore.activityEnum.status
    state.type = commonStore.activityEnum.type
    return;
  }
  const res = await getActivityEnum()
  if (res.code === 1000) {
    state.status = res.data.status
    state.type = res.data.type
    commonStore.activityEnum = res.data
  }
})
</script>

<style lang="less" scoped>
.my-form {
  padding: var(--el-main-padding);
  font-size: 16px;

  .text-center {
    text-align: center;
  }

  :deep(.el-form-item) {
    margin-right: 25px;
  }
}
</style>@/api/activityManage/ActivityManage